let logger = require("pomelo-logger").getLogger(__filename);
const HandlerUtil = require("../../../utils/handlerUtil");

module.exports = function (app) {
    return new Handler(app);
};

let Handler = function (app) {
    this.app = app;
};

Handler.prototype.createSoloGameTable = function (msg, session, callback) {
    let service = this.app.get("soloTableService");
    HandlerUtil.createYuanBaoTable(this.app, service, msg, session, callback, function (msg) {
        let data = {};
        return data;
    });
};

Handler.prototype.joinYuanBaoTable = function (msg, session, callback) {
    let service = this.app.get("soloTableService");
    HandlerUtil.joinYuanBaoTable(this.app, service, msg, session, callback);
};

Handler.prototype.leaveTable = function (msg, session, callback) {
    //TODO 不懂这里的逻辑，需要和崔确认
    let soloTableService = this.app.get("soloTableService");
    soloTableService.clearConnectionState(session.frontendId, session.id);
    let table = soloTableService.findTableByPlayer(session.frontendId, session.id);
    if (!!table) {
        let players = table.getAllPlayers();
        players.forEach(function (player, index) {
            table.kick(player);
        });
    }
    callback(null, {resultCode: 0, message: "ok"});
};

// 认输
Handler.prototype.giveUp = function (msg, session, callback) {
    let soloTableService = this.app.get("soloTableService");
    let result = soloTableService.giveUp(session.frontendId, session.id, msg.amount);
    if (result.resultCode === 0) {
        this.app.rpc.account.tableRemote.changeDiamond(null, result.params, function () {
            callback(null, {resultCode: 0, message: "成功"});
            //TODO table may be invalided here
            if (!!result.table) {
                result.table.broadCast("onGiveupSoloGame", {
                    winner: result.receiverNickname,
                    loser: result.senderNickname,
                    amount: msg.amount
                });
            }
        });
    } else {
        callback(null, result);
    }
};

Handler.prototype.getReady = function (msg, session, callback) {
    let soloTableService = this.app.get("soloTableService");
    callback(null, soloTableService.getReady(session.frontendId, session.id));
};

Handler.prototype.betForDealer = function (msg, session, callback) {
    let soloTableService = this.app.get("soloTableService");
    soloTableService.betForDealer(session.frontendId, session.id, msg.multiplier);
};

Handler.prototype.bet = function (msg, session, callback) {
    let soloTableService = this.app.get("soloTableService");
    callback(null, soloTableService.bet(session.frontendId, session.id, msg.multiplier));
};

Handler.prototype.commit = function (msg, session, callback) {
    let soloTableService = this.app.get("soloTableService");
    callback(null, soloTableService.commit(session.frontendId, session.id));
};

Handler.prototype.notifyPlayers = function (msg, session, callback) {
    let soloTableService = this.app.get("soloTableService");
    callback(null, soloTableService.sendChatMessage(session.frontendId,
        session.id, msg.message));
};

